	const ipcRenderer = require('electron').ipcRenderer;

	const remote = require('electron').remote;

	const win = remote.win;

	// 获取远程项目信息
	const config = require('./config/config');
	// 获取开发环境变量
	let env = process.env.NODE_ENV;

	$('.updateModel-close,#back-btn').off('click').on('click', function() {
		uM.modalHide();
	});

	const uM = {
		modal: $('#updateModel'),
		backDrop: $('.modal-backdrop'),
		sureBtn: $('#sure-btn'),
		backBtn: $('#back-btn'),
		// 显示
		modalShow : function(html) {
			var body = $('.modal-body');
			html ? body.empty() && body.append(html)  : '';
			this.modal.removeClass('hide');
			this.backDrop.removeClass('hide');				
		},
		// 隐藏
		modalHide : function() {
			this.modal.addClass('hide');
			this.backDrop.addClass('hide');				
		},
		// 确认
		sureAction: function() {
			this.sureBtn.off('click').on('click', function() {
				uM.modalHide();
			});			
		},
		// 关闭
		backAction: function() {
			this.backBtn.off('click').on('click', function() {
				uM.modalHide();
			});			
		},
	}
		
	//版本更新
	ipcRenderer.on('asynchronous-version', (event) => {
		$.get(config[env].url+"/common/get_version",function(res){
			alert(res);
			if (res == remote.app.getVersion()) {
				uM.modalShow("<p style='text-align:center;'>您当前已是最新版本</p>");
		        uM.sureAction(); 
		        uM.backAction();
			} else {
				uM.modalShow("<p style='text-align:center;'>版已经发布，请点击<span style='color:red'>确认</span>前往网页版系统后台下载</p>");
			    uM.sureBtn.off('click').on('click', function() {
			    	//新版本跳转下载地址
					window.open('https://www.baidu.com');
				}); 
				uM.backAction();
			}
		});	
  	})

  	//退出MPS
	ipcRenderer.on('asynchronous-exit', (event) => {
	  	uM.modalShow("<p style='text-align:center;'>是否要退出应用</p>");
		uM.sureBtn.off('click').on('click', function() {
			refreshUsers();
		});  
		uM.backAction();	
  	})

  	//X事件
	ipcRenderer.on('asynchronous-close-minimize', (event) => {
	  	uM.modalShow("<p style='text-align:center;'>关闭窗口或者最小化？</p>"); 
		uM.sureBtn.text('最小化')
		uM.sureBtn.off('click').on('click', function() {
			uM.backBtn.text('取消');
			uM.sureBtn.text('确认');
			ipcRenderer.send('asynchronous-hiden');
			uM.modalHide();
		}); 
		uM.backBtn.text('关闭')
		uM.backBtn.off('click').on('click', function() {
			refreshUsers();
		}); 	
  	})


  	// 刷新用户信息
	function refreshUsers() {
		var url = config[env].url+'logout';
	    $.get(url,'',function(res,status){
	    	//像主进程发送消息,修改是否关闭初始值
	    	ipcRenderer.send('asynchronous-Initialization-close');
	    	//关闭窗口
	    	remote.app.quit();
	    })	
	}



// 网络监测,网络恢复时进入登录页面
let monitoringStatus = true;//网络正常/网络异常
let useState = true;//正常使用/已经断开
$.ajaxSetup({  
    async : false
}); 
function monitoringNetwork() {
	var url = $(window.parent.document).find("#pageIframe").attr("src");//获取入口url
	var i = 0;
    $.get(config[env].url+"/common/get_version",function(res,status){
    	i = 1;
    	monitoringStatus = true;
    })
    if (0 == i) {
		monitoringStatus = false;
		useState = false;
    }
    if (false == useState && true == monitoringStatus) {
		document.getElementById("pageIframe").src= url;
    	useState = true;
    }
}

{
	$('#pageIframe').attr('src', config[env].url);
	setInterval(function() {
		monitoringNetwork();
	},10000)
}